package com.company.base.web.tags;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;

/**
 * 时间标签 (用法<dg:time date="${notice.sendTime}" type="1" />)
 * 
 * @author Jeremy.Tan
 * @time:2012-6-8 
 */
public class TimeTag extends SimpleTagSupport{
	private Date date;

	private String format;

	/**
	 * 显示的时间格式
	 * 1:2012-04-13
	 * 2:2012-04-13 14:56:27
	 * 3:本年不显年
	 * 4:X年前/X月前/X小时前
	 * 5:X天
	 */
	private int type;

	private static long SECOND_LONG = 1000l;

	private static long MINUTE_LONG = 60 * SECOND_LONG;

	private static long HOUR_LONG = 60 * MINUTE_LONG;

	private static long DAY_LONG = 24 * HOUR_LONG;
	
	private static long MONTH_LONG=30*DAY_LONG;
	
	private static long YEAR_LONG=365*DAY_LONG;

	@Override
	public void doTag() throws JspException, IOException {
		JspWriter out = getJspContext().getOut();
		try {
			String html = generateHTML();
			out.print(html);
		} catch (IOException e) {
			throw new JspException(e);
		}
	}

	@SuppressWarnings("deprecation")
	private String generateHTML() throws UnsupportedEncodingException {
		if (date == null) {
			return "";
		}

		if (type == 1) {
			return new SimpleDateFormat("yyyy-MM-dd").format(date);
		}

		if (type == 2) {
			return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
		}

		StringBuffer result = new StringBuffer();
		Calendar now = Calendar.getInstance();
		Calendar pre = Calendar.getInstance();
		pre.setTime(date);
		long nowTime = now.getTimeInMillis();
		long preTime = pre.getTimeInMillis();

		if (type == 3) {
			if (nowTime < preTime) {// 如果是未来的时间显示 5分钟前
				return result.append(5).append("分钟前").toString();
			}
			String time = new StringBuffer().append(
					pushZero(pre.get(Calendar.HOUR_OF_DAY))).append(':')
					.append(pushZero(pre.get(Calendar.MINUTE))).toString();
			if (nowTime / DAY_LONG == preTime / DAY_LONG) {
				return time;
			}
			String daytime = new StringBuffer().append(
					pushZero(pre.get(Calendar.MONTH) + 1)).append('-').append(
					pushZero(pre.get(Calendar.DATE))).append(' ').append(time)
					.toString();
			int year = 1900 + date.getYear();
			if (year == now.get(Calendar.YEAR)) {
				return daytime;
			}
			return result.append(year).append('-').append(daytime).toString();
		}

		if (type == 4) {
			long timeDiff = nowTime - preTime;
			// 如果是未来的时间显示 5分钟前
			if (timeDiff < 0) {
				return result.append(5).append("分钟前").toString();
			}
			long yearDiff=timeDiff/YEAR_LONG;
			if(yearDiff>0){
				return result.append(yearDiff).append("年前").toString();
			}
			long monthDiff=timeDiff/MONTH_LONG;
			if(monthDiff>0){
				return result.append(monthDiff).append("月前").toString();
			}
			long dayDiff=timeDiff/DAY_LONG;
			if(dayDiff>0){
				return result.append(dayDiff).append("天前").toString();
			}
			long hourDiff = timeDiff / HOUR_LONG;
			if(hourDiff>0){
				return result.append(hourDiff).append("小时前").toString();
			}
			long minuteDiff = timeDiff / MINUTE_LONG;
			if(minuteDiff>0){
				return result.append(minuteDiff).append("分钟前").toString();
			}
			long secondDiff = timeDiff / SECOND_LONG;
			return result.append(secondDiff == 0 ? 1 : secondDiff).append("秒前").toString();
		}
		
		if(type==5){
			long timeDiff = nowTime - preTime;
			// 如果是未来的时间显示 0天
			if (timeDiff < 0) {
				return result.append("0天").toString();
			}
			long dayDiff=timeDiff/DAY_LONG;
			return result.append(dayDiff).append("天").toString();
		}
		
		return null;
	}

	public void setFormat(String format) {
		this.format = format;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public String getFormat() {
		return format;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public String pushZero(int i) {
		return (i < 10 ? "0" : "") + i;
	}
}

